package com.uzai.console.service.tb.tbstoreblacklist.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.enums.PlatformTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.tb.tbstoreblacklist.StoreBlacklistPrvIdDto;
import com.uzai.console.dto.tb.tbstoreblacklist.StoreBlacklistPrvInfoDto;
import com.uzai.console.dto.tb.tbstoreblacklist.StoreBlacklistPrvQuery;
import com.uzai.console.entity.StoreBlacklistPrv;
import com.uzai.console.entity.StoreBlacklistPub;
import com.uzai.console.mapper.StoreBlacklistPrvMapper;
import com.uzai.console.mapper.StoreBlacklistPubMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.tb.tborder.TbOrderApiService;
import com.uzai.console.service.user.UserService;
import com.uzai.console.service.tb.tbstoreblacklist.StoreBlacklistPrvService;
import com.uzai.console.vo.tb.tbstoreblacklist.StoreBlacklistPrvInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 店铺私有黑名单
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
public class StoreBlacklistPrvServiceImpl implements StoreBlacklistPrvService {

    @Autowired
    private StoreBlacklistPrvMapper storeBlacklistPrvMapper;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private UserService userService;
    @Autowired
    private StoreBlacklistPubMapper storeBlacklistPubMapper;
    @Autowired
    private TbOrderApiService tbOrderApiService;

    /**
     * 查询私有黑名单列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param storeBlacklistPrvQuery
     * @return null
     */
    public Page<StoreBlacklistPrvInfoVo> findStoreBlacklistPrvList(StoreBlacklistPrvQuery storeBlacklistPrvQuery){
        //新建返回到页面的LIST对象
        List<StoreBlacklistPrvInfoVo> storeBlacklistPrvPageVoList = new ArrayList<>();

        //执行分页查询对象
        Page<StoreBlacklistPrv> page = new Page<>();
        storeBlacklistPrvQuery.convert(page);
        ArrayList<StoreBlacklistPrv> storeBlacklistPrvList =  storeBlacklistPrvMapper.findByList(page, storeBlacklistPrvQuery);
        if(storeBlacklistPrvList != null && storeBlacklistPrvList.size() > 0){
            for (StoreBlacklistPrv storeBlacklistPrv : storeBlacklistPrvList){
                StoreBlacklistPrvInfoVo storeBlacklistPrvPageVo = new StoreBlacklistPrvInfoVo();
                BeanUtils.copyProperties(storeBlacklistPrv, storeBlacklistPrvPageVo);
                //查询是否共享全网黑名单
                StoreBlacklistPub storeBlacklistPub = storeBlacklistPubMapper.selectByStoreIdAndMerId(storeBlacklistPrv.getStoreId(), storeBlacklistPrv.getMerId());
                if(storeBlacklistPub != null){
                    storeBlacklistPrvPageVo.setShareStatus(1);  //已经共享
                }else{
                    storeBlacklistPrvPageVo.setShareStatus(0);  //未共享
                }
                storeBlacklistPrvPageVoList.add(storeBlacklistPrvPageVo);
            }
        }
        //返回到页面分页对象
        Page<StoreBlacklistPrvInfoVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(storeBlacklistPrvPageVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 添加私有黑名单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param storeBlacklistPrvInfoDto
     * @return null
     */
    @Transactional
    public void addStoreBlacklistPrv(StoreBlacklistPrvInfoDto storeBlacklistPrvInfoDto){

        if(storeBlacklistPrvInfoDto.getType() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入黑名单类型");
        }

        if(StringUtils.isBlank(storeBlacklistPrvInfoDto.getItemId())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入商品id");
        }

        //调用淘宝接口，通过商品id获取店铺信息
        //获取店铺id
        Map<String,Object> storeMap = tbOrderApiService.findStore(storeBlacklistPrvInfoDto.getItemId());
        if (storeMap == null || storeMap.get("storeId") == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "本商品未参加淘宝客，不能添加黑名单");
        }

        String storeName = (String) storeMap.get("storeName");
//        if (storeName!=null && (storeName.contains("旗舰店") || storeName.contains("专卖店")
//                || storeName.contains("天猫店") || storeName.contains("专营店"))){
//            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此旗舰店、专卖店、天猫店、专营店等类型不可添加到店铺黑名单");
//        }

        StoreBlacklistPrv storeBlacklistPrv = storeBlacklistPrvMapper.selectByStoreIdAndMerId(String.valueOf(storeMap.get("storeId")), storeBlacklistPrvInfoDto.getMerId());
        if(storeBlacklistPrv != null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该商品所在店铺已经在黑名单中");
        }

        storeBlacklistPrv = new StoreBlacklistPrv();
        BeanUtils.copyProperties(storeBlacklistPrvInfoDto, storeBlacklistPrv);
        Integer now = DateUtil.getNowTime();
        Long replyId = IdWorker.getId();
        storeBlacklistPrv.setId(replyId);
        storeBlacklistPrv.setStoreId(String.valueOf(storeMap.get("storeId")));
        storeBlacklistPrv.setStoreName(storeName);
        storeBlacklistPrv.setCreateTime(now);
        storeBlacklistPrv.setUpdateTime(now);
        //默认为淘宝店铺黑名单
        storeBlacklistPrv.setPlatformType(PlatformTypeEnum.PT_TAOBAO.getKey());
        storeBlacklistPrvMapper.insert(storeBlacklistPrv);

        //是否共享全网黑名单
        if(storeBlacklistPrvInfoDto.getSharePubFlag() != null && storeBlacklistPrvInfoDto.getSharePubFlag() == 1){
            if (storeName!=null && (storeName.contains("旗舰店") || storeName.contains("专卖店")
                    || storeName.contains("天猫店") || storeName.contains("专营店") || storeName.contains("天天特卖工厂店"))){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此旗舰店、专卖店、专营店、天猫店、天天特卖工厂店等类型不允许加入到店铺全网黑名单");
            }

            StoreBlacklistPub storeBlacklistPub = storeBlacklistPubMapper.selectByStoreId(storeBlacklistPrv.getStoreId());
            //全网黑名单中不存在
            if(storeBlacklistPub == null){
                storeBlacklistPub = new StoreBlacklistPub();
                BeanUtils.copyProperties(storeBlacklistPrv, storeBlacklistPub);
                storeBlacklistPub.setId(IdWorker.getId());
                storeBlacklistPub.setCreateTime(now);
                storeBlacklistPub.setUpdateTime(now);
                //增加到全网黑名单
                storeBlacklistPubMapper.insert(storeBlacklistPub);
            }
        }
    }

    /**
     * 修改私有黑名单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param storeBlacklistPrvInfoDto
     * @return null
     */
    @Transactional
    public void updateStoreBlacklistPrv(StoreBlacklistPrvInfoDto storeBlacklistPrvInfoDto){

        if(storeBlacklistPrvInfoDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        if(storeBlacklistPrvInfoDto.getType() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入黑名单类型");
        }

        if(StringUtils.isBlank(storeBlacklistPrvInfoDto.getItemId())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入商品id");
        }

        //调用淘宝接口，通过商品id获取店铺信息
        // 获取店铺id
        Map<String,Object> storeMap = tbOrderApiService.findStore(storeBlacklistPrvInfoDto.getItemId());
        if (storeMap == null || storeMap.get("storeId") == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "本商品未参加淘宝客，不能添加黑名单");
        }

        String storeName = (String) storeMap.get("storeName");
//        if (storeName!=null && (storeName.contains("旗舰店") || storeName.contains("专卖店")
//                || storeName.contains("天猫店") || storeName.contains("专营店"))){
//            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此旗舰店、专卖店、天猫店、专营店等类型不可添加到店铺黑名单");
//        }


        StoreBlacklistPrv storeBlacklistPrv = storeBlacklistPrvMapper.selectById(storeBlacklistPrvInfoDto.getId(), storeBlacklistPrvInfoDto.getMerId());

        if(storeBlacklistPrv == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该私有黑名单不存在");
        }

        Integer now = DateUtil.getNowTime();
        storeBlacklistPrv.setUpdateTime(now);
        storeBlacklistPrv.setType(storeBlacklistPrvInfoDto.getType());
        storeBlacklistPrv.setStoreId(String.valueOf(storeMap.get("storeId")));
        storeBlacklistPrv.setStoreName(storeName);

        storeBlacklistPrvMapper.updateById(storeBlacklistPrv);
    }

    /**
     * 移除黑名单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param storeBlacklistPrvIdDto
     * @return null
     */
    @Transactional
    public void deleteStoreBlacklistPrv(StoreBlacklistPrvIdDto storeBlacklistPrvIdDto){

        if(storeBlacklistPrvIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        StoreBlacklistPrv storeBlacklistPrv = storeBlacklistPrvMapper.selectById(storeBlacklistPrvIdDto.getId(), storeBlacklistPrvIdDto.getMerId());

        if(storeBlacklistPrv == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此私有黑名单不存在");
        }

        //删除全网黑名单
        StoreBlacklistPub storeBlacklistPub = storeBlacklistPubMapper.selectByStoreIdAndMerId(storeBlacklistPrv.getStoreId(), storeBlacklistPrv.getMerId());
        if(storeBlacklistPub != null){
            storeBlacklistPubMapper.deleteById(storeBlacklistPub.getId(), storeBlacklistPrvIdDto.getMerId());
        }

        //删除私有黑名单
        storeBlacklistPrvMapper.deleteById(storeBlacklistPrvIdDto.getId(), storeBlacklistPrvIdDto.getMerId());
    }

    /**
     * 查询私有黑名单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param storeBlacklistPrvIdDto
     * @return null
     */
    public StoreBlacklistPrvInfoVo findStoreBlacklistPrvInfo(StoreBlacklistPrvIdDto storeBlacklistPrvIdDto){
        if(storeBlacklistPrvIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        StoreBlacklistPrv storeBlacklistPrv = storeBlacklistPrvMapper.selectById(storeBlacklistPrvIdDto.getId(), storeBlacklistPrvIdDto.getMerId());

        if(storeBlacklistPrv == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该私有黑名单不存在");
        }

        StoreBlacklistPrvInfoVo storeBlacklistPrvInfoVo = new StoreBlacklistPrvInfoVo();
        BeanUtils.copyProperties(storeBlacklistPrv, storeBlacklistPrvInfoVo);
        return storeBlacklistPrvInfoVo;
    }

    /**
     * 共享全网黑名单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param storeBlacklistPrvIdDto
     * @return null
     */
    public void shareStoreBlacklistPub(StoreBlacklistPrvIdDto storeBlacklistPrvIdDto){
        if(storeBlacklistPrvIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        StoreBlacklistPrv storeBlacklistPrv = storeBlacklistPrvMapper.selectById(storeBlacklistPrvIdDto.getId(), storeBlacklistPrvIdDto.getMerId());

        if(storeBlacklistPrv == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该私有黑名单不存在");
        }

        String storeName = storeBlacklistPrv.getStoreName();
        if (storeName!=null && (storeName.contains("旗舰店") || storeName.contains("专卖店")
                || storeName.contains("天猫店") || storeName.contains("专营店") || storeName.contains("天天特卖工厂店"))){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此旗舰店、专卖店、专营店、天猫店、天天特卖工厂店等类型不允许加入到店铺全网黑名单");
        }

        StoreBlacklistPub storeBlacklistPub = storeBlacklistPubMapper.selectByStoreId(storeBlacklistPrv.getStoreId());
        //全网黑名单中不存在
        if(storeBlacklistPub == null){
            storeBlacklistPub = new StoreBlacklistPub();
            BeanUtils.copyProperties(storeBlacklistPrv, storeBlacklistPub);
            storeBlacklistPub.setId(IdWorker.getId());
            Integer now = DateUtil.getNowTime();
            storeBlacklistPub.setCreateTime(now);
            storeBlacklistPub.setUpdateTime(now);
            //增加到全网黑名单
            storeBlacklistPubMapper.insert(storeBlacklistPub);
        }else{
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该店铺已经在共享黑名单里");
        }
    }

    /**
     * 取消全网黑名单
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param storeBlacklistPrvIdDto
     * @return null
     */
    public void cancelStoreBlacklistPub(StoreBlacklistPrvIdDto storeBlacklistPrvIdDto){
        if(storeBlacklistPrvIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        StoreBlacklistPrv storeBlacklistPrv = storeBlacklistPrvMapper.selectById(storeBlacklistPrvIdDto.getId(), storeBlacklistPrvIdDto.getMerId());

        if(storeBlacklistPrv == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该私有黑名单不存在");
        }

        StoreBlacklistPub storeBlacklistPub = storeBlacklistPubMapper.selectByStoreIdAndMerId(storeBlacklistPrv.getStoreId(), storeBlacklistPrv.getMerId());
        //全网黑名单中存在
        if(storeBlacklistPrv != null){
            storeBlacklistPubMapper.deleteById(storeBlacklistPub.getId(), storeBlacklistPrvIdDto.getMerId());
        }
    }
}
